var t;  //set time out thoi gian quay thuong
function check_isNumber($value){
    var reg = /^\d+$/;
    return (reg.test($value));    
}

function showForm(divId){
    if(document.getElementById(divId).style.display=="none"){
        document.getElementById(divId).style.display = "block"; 
    }else{
        document.getElementById(divId).style.display = "none";
    }
}
function in_array (needle, haystack, argStrict) {
    var key = '',        
    strict = !! argStrict;
    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {                
                return true;
            }
        }
    } else {
        for (key in haystack) {            
            if (haystack[key] == needle) {
                return true;
            }
        }
    } 
    return false;
}

// trim, rtrim, ltrim
function trim(str, chr) {
    var rgxtrim = (!chr) ? new RegExp('^\\s+|\\s+$', 'g') : new RegExp('^'+chr+'+|'+chr+'+$', 'g');
    return str.replace(rgxtrim, '');
}
function rtrim(str, chr) {
    var rgxtrim = (!chr) ? new RegExp('\\s+$') : new RegExp(chr+'+$');
    return str.replace(rgxtrim, '');
}
function ltrim(str, chr) {
    var rgxtrim = (!chr) ? new RegExp('^\\s+') : new RegExp('^'+chr+'+');
    return str.replace(rgxtrim, '');
}


function setHtmlVideo(path,width,height){
    var html = '';
    html += '<object width="'+width+'" height="'+height+'" type="video/quicktime" data="'+path+'">';
    html += '<param value="'+path+'" name="src">';
    html += '</object>';
    return html;
}

function loadketquamienbac(strUrl,divId){
    $.ajax({
        type: "POST",
        url: strUrl,
        data: "",
        success: function(msg){
            var response = $.parseJSON(msg);
            $("#"+divId).html(response.ketqua);
            $("#show_loto").html(response.loto);
        }
    });
}
function loadKetquaMiennam(strUrl,province_id,divId){ 
    $.ajax({
        type: "POST",
        url: strUrl,
        data: {province_id:province_id},
        success: function(msg){
            var response = $.parseJSON(msg);
            $("#"+divId).html(response.ketqua);
            $("#show_loto").html(response.loto);
        }
    });
}
function loadKetquaMientrung(strUrl,province_id,divId){ 
    $.ajax({
        type: "POST",
        url: strUrl,
        data: {province_id:province_id},
        success: function(msg){
            var response = $.parseJSON(msg);
            $("#"+divId).html(response.ketqua);
            $("#show_loto").html(response.loto);
        }
    });
}
function format_number_post(num){
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
        num = num.substring(0,num.length-(4*i+3))+'.'+
    num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') + num);
}

function reloadCaptcha(imageName,path){
    var randomnumber=Math.floor(Math.random()*101001); // generate a random number to add to image url to prevent caching
    var src = path+'/captcha.php?num=' + randomnumber;
    $('img[name='+imageName+']').attr('src',src);  // change image src to the same url but with the random number on the end
}

function setCookie(c_name,value,exdays){
    var exdate=new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
    document.cookie=c_name + "=" + c_value;
}


function timedCountMB()
{    
    stopCount();
    //giai dac biet
    var c = Math.floor(Math.random()*90000) + 10000;     
    document.getElementById('db').innerHTML = c;

    //giai nhat
    var c1 = Math.floor(Math.random()*c) + 10000;
    document.getElementById('g1').innerHTML=c1;

    //giai hai
    var c2_1 = Math.floor(Math.random()*c1) + 10000;
    document.getElementById('g2_1').innerHTML=c2_1;
    var c2_2 = Math.floor(Math.random()*c2_1) + 10000;
    document.getElementById('g2_2').innerHTML=c2_2;

    //giai ba
    var c3_1 = Math.floor(Math.random()*c2_2) + 10000;
    document.getElementById('g3_1').innerHTML=c3_1;
    var c3_2 = Math.floor(Math.random()*c3_1) + 10000;
    document.getElementById('g3_2').innerHTML=c3_2;
    var c3_3 = Math.floor(Math.random()*c3_2) + 10000;
    document.getElementById('g3_3').innerHTML=c3_3;
    var c3_4 = Math.floor(Math.random()*c3_3) + 10000;
    document.getElementById('g3_4').innerHTML=c3_4;
    var c3_5 = Math.floor(Math.random()*c3_4) + 10000;
    document.getElementById('g3_5').innerHTML=c3_5;
    var c3_6 = Math.floor(Math.random()*c3_5) + 10000;
    document.getElementById('g3_6').innerHTML=c3_6;

    //giai tu    
    var c4_1 = Math.floor(Math.random()*9000) + 1000;
    document.getElementById('g4_1').innerHTML=c4_1;
    var c4_2 = Math.floor(Math.random()*c4_1) + 1000;
    document.getElementById('g4_2').innerHTML=c4_2;
    var c4_3 = Math.floor(Math.random()*c4_2) + 1000;
    document.getElementById('g4_3').innerHTML=c4_3;
    var c4_4 = Math.floor(Math.random()*c4_3) + 1000;
    document.getElementById('g4_4').innerHTML=c4_4;

    //giai 5
    var c5_1 = Math.floor(Math.random()*c4_4) + 1000;
    document.getElementById('g5_1').innerHTML=c5_1;
    var c5_2 = Math.floor(Math.random()*c5_1) + 1000;
    document.getElementById('g5_2').innerHTML=c5_2;
    var c5_3 = Math.floor(Math.random()*c5_2) + 1000;
    document.getElementById('g5_3').innerHTML=c5_3;
    var c5_4 = Math.floor(Math.random()*c5_3) + 1000;
    document.getElementById('g5_4').innerHTML=c5_4;
    var c5_5 = Math.floor(Math.random()*c5_4) + 1000;
    document.getElementById('g5_5').innerHTML=c5_5;
    var c5_6 = Math.floor(Math.random()*c5_5) + 1000;
    document.getElementById('g5_6').innerHTML=c5_6;

    //giai 6
    var c6_1 = Math.floor(Math.random()*900) + 100;
    document.getElementById('g6_1').innerHTML=c6_1;
    var c6_2 = Math.floor(Math.random()*c6_1) + 100;
    document.getElementById('g6_2').innerHTML=c6_2;
    var c6_3 = Math.floor(Math.random()*c6_2) + 100;
    document.getElementById('g6_3').innerHTML=c6_3;

    //giai 7
    var c7_1 = Math.floor(Math.random()*90) + 10;
    document.getElementById('g7_1').innerHTML=c7_1;
    var c7_2 = Math.floor(Math.random()*c7_1) + 10;
    document.getElementById('g7_2').innerHTML=c7_2;
    var c7_3 = Math.floor(Math.random()*c7_2) + 10;
    document.getElementById('g7_3').innerHTML=c7_3;
    var c7_4 = Math.floor(Math.random()*c7_3) + 10;
    document.getElementById('g7_4').innerHTML=c7_4;
    t=setTimeout("timedCountMB()",100);    
}

function quaythuongMB(id1,id2){
    var time_quay = 2000;
    var c7 = Math.floor(Math.random()*90) + 10;  
    var c6 = Math.floor(Math.random()*900) + 100;
    var c5 = Math.floor(Math.random()*9000) + 1000;
    var c4 = Math.floor(Math.random()*9000) + 1000;
    var c3 = Math.floor(Math.random()*90000) + 10000;
    var c2 = Math.floor(Math.random()*90000) + 10000;
    var c1 = Math.floor(Math.random()*90000) + 10000;
    var c0 = Math.floor(Math.random()*90000) + 10000;   

    if(id1==7){
        $('#g'+id1+'_'+id2).removeClass('loading').html(c7);
        if(id2 <4){
            t=setTimeout("quaythuongMB("+id1+","+(id2+1)+")",time_quay);
        }else{
            t=setTimeout("quaythuongMB(6,1)",time_quay);
        }


    }
    if(id1==6){
        $('#g'+id1+'_'+id2).removeClass('loading').html(c6);
        if(id2 <3){
            t=setTimeout("quaythuongMB("+id1+","+(id2+1)+")",time_quay);
        }else{
            t=setTimeout("quaythuongMB(5,1)",time_quay);
        }

    }
    if(id1==5){
        $('#g'+id1+'_'+id2).removeClass('loading').html(c5);
        if(id2 <6){
            t=setTimeout("quaythuongMB("+id1+","+(id2+1)+")",time_quay);
        }else{
            t=setTimeout("quaythuongMB(4,1)",time_quay);
        }

    }
    if(id1==4){
        $('#g'+id1+'_'+id2).removeClass('loading').html(c4);
        if(id2 <4){
            t=setTimeout("quaythuongMB("+id1+","+(id2+1)+")",time_quay);
        }else{
            t=setTimeout("quaythuongMB(3,1)",time_quay);
        }
    }
    if(id1==3){
        $('#g'+id1+'_'+id2).removeClass('loading').html(c3);
        if(id2 <6){
            t=setTimeout("quaythuongMB("+id1+","+(id2+1)+")",time_quay);
        }else{
            t=setTimeout("quaythuongMB(2,1)",time_quay);
        }
    }
    if(id1==2){
        $('#g'+id1+'_'+id2).removeClass('loading').html(c2);
        if(id2 <2){
            t=setTimeout("quaythuongMB("+id1+","+(id2+1)+")",time_quay);
        }else{
            t=setTimeout("quaythuongMB(1,1)",time_quay);
        }
    }
    if(id1==1){
        $('#g1').removeClass('loading').html(c1);
        t=setTimeout("quaythuongMB(0,1)",time_quay);  
    }
    if(id1==0){
        $('#db').removeClass('loading').html(c0);  
        stopCount();          
    }
}

function timedCountMNMT()
{    
    stopCount();
    //giai dac biet
    var c = Math.floor(Math.random()*90000) + 100000;     
    document.getElementById('db').innerHTML = c;

    //giai nhat
    var c1 = Math.floor(Math.random()*c) + 10000;
    document.getElementById('g1').innerHTML=c1;

    //giai hai
    var c2 = Math.floor(Math.random()*c1) + 10000;
    document.getElementById('g2').innerHTML=c2;

    //giai ba
    var c3_1 = Math.floor(Math.random()*c2) + 10000;
    document.getElementById('g3_1').innerHTML=c3_1;
    var c3_2 = Math.floor(Math.random()*c3_1) + 10000;
    document.getElementById('g3_2').innerHTML=c3_2;

    //giai tu    
    var c4_1 = Math.floor(Math.random()*9000) + 10000;
    document.getElementById('g4_1').innerHTML=c4_1;
    var c4_2 = Math.floor(Math.random()*c4_1) + 10000;
    document.getElementById('g4_2').innerHTML=c4_2;
    var c4_3 = Math.floor(Math.random()*c4_2) + 10000;
    document.getElementById('g4_3').innerHTML=c4_3;
    var c4_4 = Math.floor(Math.random()*c4_3) + 10000;
    document.getElementById('g4_4').innerHTML=c4_4;
    var c4_5 = Math.floor(Math.random()*c4_4) + 10000;
    document.getElementById('g4_5').innerHTML=c4_2;
    var c4_6 = Math.floor(Math.random()*c4_5) + 10000;
    document.getElementById('g4_6').innerHTML=c4_3;
    var c4_7 = Math.floor(Math.random()*c4_6) + 10000;
    document.getElementById('g4_7').innerHTML=c4_4;

    //giai 5
    var c5 = Math.floor(Math.random()*c4_7) + 1000;
    document.getElementById('g5').innerHTML=c5;


    //giai 6
    var c6_1 = Math.floor(Math.random()*900) + 1000;
    document.getElementById('g6_1').innerHTML=c6_1;
    var c6_2 = Math.floor(Math.random()*c6_1) + 1000;
    document.getElementById('g6_2').innerHTML=c6_2;
    var c6_3 = Math.floor(Math.random()*c6_2) + 1000;
    document.getElementById('g6_3').innerHTML=c6_3;

    //giai 7
    var c7 = Math.floor(Math.random()*90) + 100;
    document.getElementById('g7').innerHTML=c7;
    //giai 8
    var c8 = Math.floor(Math.random()*90) + 10;
    document.getElementById('g8').innerHTML=c8;
    t=setTimeout("timedCountMNMT()",100);    
}
function quaythuongMNMT(id1,id2){
    var time_quay = 2000;
    var c8 = Math.floor(Math.random()*90) + 10;  
    var c7 = Math.floor(Math.random()*900) + 100;  
    var c6 = Math.floor(Math.random()*9000) + 1000;
    var c5 = Math.floor(Math.random()*9000) + 1000;
    var c4 = Math.floor(Math.random()*90000) + 10000;
    var c3 = Math.floor(Math.random()*90000) + 10000;
    var c2 = Math.floor(Math.random()*90000) + 10000;
    var c1 = Math.floor(Math.random()*90000) + 10000;
    var c0 = Math.floor(Math.random()*900000) + 100000;   
    if(id1==8){
        $('#g8').removeClass('loading').html(c8);
        t=setTimeout("quaythuongMNMT(7,1)",time_quay);    
    }
    if(id1==7){
        $('#g7').removeClass('loading').html(c7);
        t=setTimeout("quaythuongMNMT(6,1)",time_quay);
    }
    if(id1==6){
        $('#g'+id1+'_'+id2).removeClass('loading').html(c6);
        if(id2 <3){
            t=setTimeout("quaythuongMNMT("+id1+","+(id2+1)+")",time_quay);
        }else{
            t=setTimeout("quaythuongMNMT(5,1)",time_quay);
        }
    }
    if(id1==5){
        $('#g5').removeClass('loading').html(c5);
        t=setTimeout("quaythuongMNMT(4,1)",time_quay);
    }
    if(id1==4){
        $('#g'+id1+'_'+id2).removeClass('loading').html(c4);
        if(id2 <7){
            t=setTimeout("quaythuongMNMT("+id1+","+(id2+1)+")",time_quay);
        }else{
            t=setTimeout("quaythuongMNMT(3,1)",time_quay);
        }
    }
    if(id1==3){
        $('#g'+id1+'_'+id2).removeClass('loading').html(c3);
        if(id2 <2){
            t=setTimeout("quaythuongMNMT("+id1+","+(id2+1)+")",time_quay);
        }else{
            t=setTimeout("quaythuongMNMT(2,1)",time_quay);
        }
    }
    if(id1==2){
        $('#g2').removeClass('loading').html(c2);
        t=setTimeout("quaythuongMNMT(1,1)",time_quay);
    }
    if(id1==1){
        $('#g1').removeClass('loading').html(c1);
        t=setTimeout("quaythuongMNMT(0,1)",time_quay);  
    }
    if(id1==0){
        $('#db').removeClass('loading').html(c0);  
        stopCount();          
    }
}

function stopCount()
{
    clearTimeout(t);
}

